package com.http.intg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.AsyncRestTemplate;

import java.time.Duration;

@Configuration
public class AsyncHttpServiceConfig {
    public static final Logger log = LoggerFactory.getLogger(AsyncHttpServiceConfig.class);

    @Value("${com.http.link:}")
    private String httpUrl;

    @Value("${com.http.connect-timeout:}")
    private int connectTimeout;

    @Value("${com.http.read-timeout:}")
    private int readTimeout;

    @Bean
    public AsyncHttpServiceClient asynchttpServiceClient(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //设置链接超时时间
        factory.setConnectTimeout(this.connectTimeout);
        //设置读取资料超时时间
        factory.setReadTimeout(this.readTimeout);
        //设置异步任务（线程不会重用，每次调用时都会重新启动一个新的线程）
        factory.setTaskExecutor(new SimpleAsyncTaskExecutor());
        AsyncHttpServiceClient httpServiceClient = new AsyncHttpServiceClient();
        httpServiceClient.setAsyncRestTemplate(new AsyncRestTemplate(factory));
        return httpServiceClient;
    }
}
